package bb.lanxing.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import bb.lanxing.R;
import bb.lanxing.model.database.RouteSlope;
import bb.lanxing.util.CommonUtil;
import bb.lanxing.util.DateUtil;
import java.text.DecimalFormat;
import java.util.List;

public class RouteSlopeAdapter extends RecyclerView.Adapter<RouteSlopeAdapter.SlopeViewHolder> {
    private DecimalFormat decimalFormat = new DecimalFormat("0.0%");
    private List<RouteSlope> slopes;

    public RouteSlopeAdapter(List<RouteSlope> slopes) {
        this.slopes = slopes;
    }

    @Override
    public SlopeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SlopeViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_route_slope_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(SlopeViewHolder holder, int position) {
        RouteSlope routeSlope = this.slopes.get(position);
        Context context = holder.itemView.getContext();
        holder.levelView.setText(routeSlope.getLevel());
        holder.distanceView.setText(CommonUtil.getFormatDistance(routeSlope.getDistance()));
        holder.avgGradeView.setText(this.decimalFormat.format(routeSlope.getAvgGrade()));
        holder.elevationGainView.setText(String.valueOf(Math.round(routeSlope.getElevationGain())));
        holder.distanceIntervalView.setText(context.getString(R.string.route_chart_table_item_interval_placeholder, CommonUtil.getFormatDistance(routeSlope.getStartDist()), CommonUtil.getFormatDistance(routeSlope.getEndDist())));
        holder.altitudeIntervalView.setText(context.getString(R.string.route_chart_table_item_interval_placeholder, String.valueOf(Math.round(routeSlope.getStartAltitude())), String.valueOf(Math.round(routeSlope.getEndAltitude()))));
        holder.estimatedTimeView.setText(DateUtil.format(routeSlope.getEstimatedTime(), 2));
    }

    @Override
    public int getItemCount() {
        List<RouteSlope> list = this.slopes;
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    public class SlopeViewHolder extends RecyclerView.ViewHolder {
        TextView altitudeIntervalView;
        TextView avgGradeView;
        TextView distanceIntervalView;
        TextView distanceView;
        TextView elevationGainView;
        TextView estimatedTimeView;
        TextView levelView;

        SlopeViewHolder(View itemView) {
            super(itemView);
            this.levelView = (TextView) itemView.findViewById(R.id.item_slope_level);
            this.distanceView = (TextView) itemView.findViewById(R.id.item_slope_distance);
            this.avgGradeView = (TextView) itemView.findViewById(R.id.item_slope_avg_grade);
            this.elevationGainView = (TextView) itemView.findViewById(R.id.item_slope_elevation_gain);
            this.distanceIntervalView = (TextView) itemView.findViewById(R.id.item_slope_distance_interval);
            this.altitudeIntervalView = (TextView) itemView.findViewById(R.id.item_slope_altitude_interval);
            this.estimatedTimeView = (TextView) itemView.findViewById(R.id.item_slope_estimated_time);
        }
    }
}
